/**
 * 
 */
package trieMatch.keywordMatcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import trieMatch.Constants;
import trieMatch.util.coll.Pair;

/***********************************************************************************************************************
 * Output formatter for keymatch results, showing URL, score and optionally the description.
 * 
 * @author mlrus
 * 
 */
public class ResultFormatter {

	public List<String> formatResult(final Collection<KMDefinition> kmdefs) {
		final List<String> answerList = new ArrayList<String>();
		int maxEmit = Constants.maxResultCount;
		int currRank = -1;
		int currScore = -1;
		for (final KMDefinition kmd : kmdefs) {
			if (kmd.score != currScore) {
				currRank++;
				currScore = kmd.score;
			}
			answerList.add(formatResult(kmd) + "  " + currRank);
			if (--maxEmit <= 0) {
				break;
			}
		}
		return answerList;
	}

	String formatResult(final KMDefinition kmd) {
		String ans;
		if (KMDefinition.showDescriptions) {
			ans = kmd.url + " " + KeyMatch.symbolToName.get(kmd.url) + " " + kmd.score;
		} else {
			ans = kmd.url + " " + kmd.score;
		}
		return ans;
	}

	public List<String> formatResults(final Collection<Pair<String, Integer>> rans) {
		final List<String> answerList = new ArrayList<String>();
		int maxEmit = Constants.maxResultCount;
		int currRank = -1;
		int currScore = -1;
		for (final Pair<String, Integer> st : rans) {
			final int score = st.t();
			if (score != currScore) {
				currRank++;
				currScore = score;
			}
			if (KMDefinition.showDescriptions) {
				final String nameFound = KeyMatch.symbolToName.get(st.s());
				answerList.add(String.format("%-20s %5d %3d %s ", st.s(), st.t(), currRank, "[" + nameFound + "]"));
			} else {
				answerList.add(String.format("%-35s %5d %3d", st.s(), st.t(), currRank));
			}
			if (maxEmit-- <= 0) {
				break;
			}
		}
		return answerList;
	}
}